<?php
// src/Pyaf/GymBundle/Controller/DefaultController.php
namespace Pyaf\GymBundle\Controller;
use Pyaf\GymBundle\Entity\Clase;
use Pyaf\GymBundle\Entity\Rutina;
use Pyaf\GymBundle\Entity\Cuota;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{   
    
    public function indexAction() {
        
        date_default_timezone_set('America/Argentina/Buenos_Aires');
        $currentDate = date("Y-m-d");
//        $currentDate = date("Y-m-17"); //de prueba
        $currentDay  = date("l");
        $dia = $this->dayToDia($currentDay);
//        $dia = 'Lunes'; //de prueba
        
        $clases = new Clase();
        $rutina = new Rutina();
        
        $em = $this->getDoctrine()->getEntityManager();
        
        $this->controlMensual();
        
        $clases = $em->getRepository('PyafGymBundle:Clase')->findAllOrderByDia($dia);
        $id_rutina = $em->getRepository('PyafGymBundle:Calendario')->findRutinaByCurrentDate("$currentDate 00:00:00");
        if(!empty($id_rutina)){
            $rutina = $em->getRepository('PyafGymBundle:Rutina')->findRutina($id_rutina->getIdRutina());
            
            $this->get('session')
                         ->setFlash(
                                        'alertaInicioSuccess',
                                        'Bienvenido!'
                                    );
            
            return $this->render('PyafGymBundle:Default:index.html.twig', 
                                array('rutina' => $rutina));
        } else {
//            "Día no habil o el día no tiene rutina asignada"
            $this->get('session')
                         ->setFlash(
                                        'alertaInicioError',
                                        'Día no habil o el día no tiene rutina asignada'
                                    );
            
            return $this->render('PyafGymBundle:Default:index.html.twig', 
                                array('rutina' => $rutina));
        }
        
    }
    
    /***************************************************************************
     * Funciones que no aportan a la vista
     **************************************************************************/
    public function dayToDia($day){
        $dias = array(
            'Lunes'=>'Monday', 'Martes'=>'Tuesday',
            'Miércoles'=>'Wednesday','Jueves'=>'Thursday',
            'Viernes'=>'Friday', 'Sábado'=>'Saturday', 'Domingo'=>'Sunday');
        
        foreach ($dias as $key => $dia) {
            if($dia == $day){
                return $key;
                exit();
            }
        }
        return null;
    }
    
    public function controlMensual() {
        date_default_timezone_set('America/Argentina/Buenos_Aires');
        $currentMonth = date("m");
        $currentYear  = date("Y");
        $cuotaMes     = date("m-Y");
        $currentDate  = date("d-m-Y");
        
        $em = $this->getDoctrine()->getEntityManager();
        $alumnosQueDebenMes = $em->getRepository('PyafGymBundle:Cuota')->AlumnosQueDebenMes($cuotaMes);
        
        if(count($alumnosQueDebenMes)>0){
            foreach ($alumnosQueDebenMes as $alumno) {
                if ($alumno->getPersona()->getHabilitado()) {
                    /*
                     * dar de alta la cuota para el alumno
                     */
                    $cuota = new Cuota();
                    $cuota->setAlumno($alumno);
                    $cuota->setMonto($alumno->getCuota());
                    $cuota->setMesAbono($cuotaMes);
                    $cuota->setEstado(false);
                    $cuota->setCodCuota($alumno->getId().date('mY'));
                    $em->persist($cuota);
                    $em->flush();
                    /*
                     * fin alta cuota
                     */            
                }
            }
        }    
    }
        
}

